package com.cs.study.homework._0314.list;


import org.w3c.dom.Node;

import java.util.Objects;

/*
底层使用双向链表存储
 */
public class MyDBLinkedList {
    // 基本结构
    // 头
    private Node head;
    // 尾
    private Node tail;
    private int size;

    // 节点结构
    public class Node {
        String value;
        Node pre;
        Node next;

        public Node(String value) {
            this.value = value;
        }
    }

    // 获取容器大小的方法
    public int size() {
        return size;
    }

    // 判断是否为空
    public boolean isEmpty() {
        return true;
    }

    // 基于内容的添加操作
    public boolean add(String value) {
        if (isEmpty()) {
            Node node = new Node(value);
            head = tail = node;
            size++;
            return true;
        }

        Node node = new Node(value);
        node.pre = tail;
        tail.next = node;
        tail = node;
        size++;
        return true;


    }

    // 基于内容的删除操作
    public boolean remove(String value) {
        if (isEmpty()) {
            throw new RuntimeException("list ie empty");
        }

        //处理头
        if (Objects.equals(head.value, value)) {
            //一个节点
            if (size == 1) {
                head = tail = null;
            } else {
                //多个节点
                head = head.next;
                head.pre = null;
            }
            size--;
            return true;
        }

        // 定义节点用于遍历
        Node mid = head;
        while (mid.next != null && !Objects.equals(mid.next.value, value)) {
            // mid后移
            mid = mid.next;
        }
        // 1.mid.next == null
        if (mid.next == null) {
            return false;
        }

        // 2.找到了
        // mid.next就是要删除的节点 有可能在中间  有可能在尾
        Node removeNode = mid.next;
        //处理尾
        if (removeNode.next == null) {
            removeNode.pre.next = null;
            tail = removeNode.pre;
            size--;
            return true;
        }

        //处理中间
        removeNode.next.pre = removeNode.pre;
        removeNode.pre.next = removeNode.next;

        return true;
    }

    // 基于内容的查找
    public boolean contains(String value) {
        if (isEmpty()) {
            throw new RuntimeException("list ie empty");
        }

        Node mid = head;
        while (mid != null) {
            if (Objects.equals(mid.value, value)) {
                return true;
            }
            mid = mid.next;
        }
        return false;
    }

    // 基于内容的修改
    public boolean set(String oldValue,String newValue){
        if (isEmpty()) {
            throw new RuntimeException("list ie empty");
        }
        Node mid = head;

        while (mid != null) {
            if (Objects.equals(mid.value,oldValue)) {
                mid.value = newValue;
                return true;
            }
            mid = mid.next;
        }
        return false;
    }

    // 基于下标的添加操作
    public boolean add(int index,String value){
        if (isEmpty()) {
            Node node = new Node(value);
            head = tail = node;
            size++;
            return true;
        }
        return true;
    }
}
